package cc.eddic.practice.concurrency;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Objects;
import java.util.zip.GZIPInputStream;

@Slf4j
public class FileCompressedTyqImpl {
    public static void main(String[] args) {
        File file = new File("D:\\SONY培训\\2023-04\\2023-04");
        if (log.isDebugEnabled()) log.debug("读取文件夹：" + file.getPath());
        File[] files = file.listFiles();
        if (log.isDebugEnabled()) {
            assert files != null;
            log.debug("file[]" + files.length);
        }
        for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
            String logPath = files[i].getName();
            byte[] buffer = new byte[1024];
            if (logPath.endsWith(".gz")) {
                try {
                    GZIPInputStream gzipIS = new GZIPInputStream(Files.newInputStream(Paths.get(files[i].getPath())));
                    String fileName = "D:\\SONY培训\\0811CompressOutPut";
                    int length;
                    FileOutputStream fos = new FileOutputStream(fileName + "\\" + i + ".txt");
                    while ((length = gzipIS.read(buffer)) > 0) {
                        fos.write(buffer, 0, length);
                    }
                    gzipIS.close();
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
